import 'package:drift/native.dart';
import 'package:drift/drift.dart' as DriftColumn;
import 'package:drift_db_viewer/drift_db_viewer.dart';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:libflutter/db/DBManager.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;

class SQLPageWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return _SQLPageState();
  }
}

class _SQLPageState extends State<SQLPageWidget> {
  final _controller = TextEditingController();

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("SQL 学习和使用"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            Text("Drift SQL 数据库的使用"),
            TextButton(
              onPressed: () async {
                var db = DBManager();
                db
                    .saveEmployee(
                        const PersonData(id: -1, title: "title123123", content: "content"))
                    .then((value) => {print("PersonData insert success ")});
              },
              child: Text("Drift 创建"),
            ),
            TextButton(
              onPressed: () async {
                var db = DBManager();
                Navigator.of(context).push(MaterialPageRoute(builder: (context) => DriftDbViewer(db)));
              },
              child: Text("Drift 表"),
            )
          ],
        ),
      ),
    );
  }
}

class Person extends DriftColumn.Table {
  DriftColumn.IntColumn get id => integer().autoIncrement()();

  DriftColumn.TextColumn get title => text().withLength(min: 6, max: 32)();

  DriftColumn.TextColumn get content => text().named('body')();

  DriftColumn.IntColumn get category => integer().nullable()();
}
